package com.xhl.consul.tool.opentracing;

import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Author CazCen
 * @Date 2021/11/25 13:33
 */
@Component
@Slf4j
public class OpentracingTest {

    public static String getTraceId() {

        Tracer tracer = GlobalTracer.get();

        ScopeManager scopeManager = tracer.scopeManager();

        Span span = scopeManager.activeSpan();

        SpanContext context = span.context();

        context.toTraceId();

        String traceId = GlobalTracer.get().scopeManager().activeSpan().context().toTraceId();

        String uID = UUID.randomUUID().toString().replace("-", "").substring(0, 16);


        log.info("traceId:{}",traceId);
        log.info("uID:{}",uID);


        return StringUtils.isEmpty(traceId)? uID:traceId;
    }

}
